<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02 axios</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.min.js"></script>
    <style>
        .table{
            width: 800px;
            margin: 50px auto;
        }
        .table .tr{
            width: 800px;
        }
        .table .tr .item{
            width: 200px;
            height: 50px;
            line-height: 40px;
            text-align: center;
            background-color: darkblue;
            color: darkorange;
            float: left;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="table">
            <div class="tr">
                用户名<input type="text" v-model="username">
                密码<input type="text" v-model="userage">
                <button @click="add">新增</button>
            </div>
            <div class="tr">
                <div class="item">ID</div>
                <div class="item">姓名</div>
                <div class="item">年龄</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el:"#app",
        data:{
            list:[],
            userage:"",
            username:"",

        },
        created(){
            axios({
                method:"get",
                url:"http://localhost:2345/userinfos"
            }).then(res => {this.list = res.data})

        },
        methods:{
            // 新增
            async add(){
                // 新增之前判断用户名是否存在

                let {data:list} = await axios({
                    method:"get",
                    url:"http://localhost:2345/userinfos",
                    params:{
                        username:this.username,
                        // userage:this.userage,
                    },
                });



                if(list.length){
                    alert("用户名已经存在");
                }else{
                    let {data} = await axios({
                        method:"post",
                        url:"http://localhost:2345/userinfos",
                        data:{
                            username:this.username,
                            userage:this.userage,
                        },
                    })
    
                    console.log(data)
                    this.list.push(data);
                }
            },
            // 删除
            async del(id){
                await axios({
                    method:"delete",
                    url:"http://localhost:2345/userinfos/"+id,
                })

                this.list = this.list.filter(item => item.id != id)

            }


        }
    })
</script>
</html>